package it.patterns.behavioral.iterator;

public class ConcreteAggregate<Item> implements Aggregate {
	private Object elements[];
	private int size;

	public ConcreteAggregate(int size) {
		this.size = size;
		elements = new Object[size];
	}

	@Override
	public int getSize() {
		return size;
	}

	@Override
	public Item get(int index) throws Exception {
		if (index >= size)
			throw new Exception("Index out of bound");
		return (Item) elements[index];
	}

	@Override
	public Iterator<Object> createIterator() {
		return new ConcreteIterator<Object>(this);
	}

	@Override
	public void add(Object value, int index) throws Exception {
		if (index >= size)
			throw new Exception("Index out of bound");
		elements[index] = value;
	}

}
